/*
 * @Author: wwssaabb
 * @Date: 2021-09-04 11:03:41
 * @LastEditTime: 2021-11-26 11:48:10
 * @FilePath: \handwritten-code\test.js
 */

const MyPromise = require('./promise')

//测试


//测试 then
/*
const p = new MyPromise((resolve, reject) => {
  resolve('success')
  reject('err')
})

p.then(res => console.log(res), err => console.log(err))
*/

//测试 异步处理+多次then
/*
const p = new MyPromise((resolve, reject) => {
  setTimeout(() => resolve('success'), 2000)
})

p.then(res => console.log('resolve', res))

p.then(value => {
  console.log(1)
  console.log('resolve', value)
})

p.then(value => {
  console.log(2)
  console.log('resolve', value)
})

p.then(value => {
  console.log(3)
  console.log('resolve', value)
})
*/

//测试 then链式调用
/*
const p = new MyPromise((resolve, reject) => {
  resolve('success')
})

function other() {
  return new MyPromise((resolve, reject) => {
    resolve('other')
  })
}

p.then(value => {
  console.log(1)
  console.log('resolve', value)
  return other()
}).then(value => {
  console.log(2)
  console.log('resolve', value)
})
*/

//测试 避免返回自己
/*
let p=new MyPromise((resolve,reject)=>{
  resolve(1)
})

const p1=p.then(value=>{
  console.log(value)
  return p1
})

p1.then(value => {
  console.log(2)
  console.log('resolve', value)
}, reason => {
  console.log(3)
  console.log(reason.message)
})
*/

//测试 执行器捕获错误，改变状态为失败并返回错误
/*
let p=new MyPromise((resolve,reject)=>{
  throw new Error('执行器错误')
})

p.then(()=>console.log('success'),err=>console.log(err))*/


//测试 执行时捕获错误，改变状态为失败并返回错误
/*
let p = new MyPromise((resolve, reject) => {
  resolve('success')
})

p.then(value => {
  console.log(1)
  console.log('resolve', value)
  throw new Error('then error')
}, reason => {
  console.log(2)
  console.log(reason.message)
}).then(value => {
  console.log(3)
  console.log(value);
}, reason => {
  console.log(4)
  console.log(reason.message)
})
*/


//测试 then参数可选
/*
const p = new MyPromise((resolve, reject) => {
  resolve('success')
})

p.then().then().then().then(value => console.log(value))

const p1 = new MyPromise((resolve, reject) => {
  reject('err')
})

p1.then().then().then(value => console.log(value), reason => console.log(reason))*/


// MyPromise.resolve().then(() => {
//   console.log(0);
//   // return MyPromise.resolve(4);
//   return MyPromise.reject(4);
// }).then((res) => {
//   console.log(res)
// })

// MyPromise.resolve().then(() => {
//   console.log(0);
//   return MyPromise.resolve(4);
// }).then((res) => {
//   console.log(res)
// })

// MyPromise.resolve().then(() => {
//   console.log(1);
// }).then(() => {
//   console.log(2);
// }).then(() => {
//   console.log(3);
// }).then(() => {
//   console.log(5);
// }).then(() => {
//   console.log(6);
// })


// let obj = {}
// let value = 1
// Object.defineProperty(obj, 'a', {
//   get() {
//     return value
//   },
//   set(newValue) {
//     value = newValue
//   }
// })

function Parent(age) {
  this.age = age
  this.say = function () {
    console.log(`我是${this.name},今年${this.age}`)
  }
}
Parent.prototype.age = 36

//原型链继承
/*
function Child(name) {
  this.name = name
}

Child.prototype = new Parent() //无法传参
const child1 = new Child('child1')
console.log(child1.age)
console.log(child1.say())
*/

//借用构造函数继承
function Child(name, age) {
  Parent.call(this, age)
  this.name = name
}

const child2 = new Child('child2', 18)
const child3 = new Child('child2')
console.log(child2.age)
console.log(child2.say())
console.log(child3.age)
console.log(child3.say())